package com.education.welco.service.impl;

import com.education.common.utils.DateUtils;
import com.education.welco.controller.OrderPaperController;
import com.education.welco.mapper.OrderPaperMapper;
import com.education.welco.mapper.OrderPaperQuestionMapper;
import com.education.welco.pojo.OrderPaper;
import com.education.welco.pojo.OrderPaperQuestion;
import com.education.welco.pojo.TClass;
import com.education.welco.pojo.vo.OrderPaperAppVO;
import com.education.welco.pojo.vo.OrderPaperVo;
import com.education.welco.service.OrderPaperService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.List;

/**
 * 试卷表 服务层实现
 *
 * @author d
 * @date 2021-03-04 15:45:18
 */
@Service
public class OrderPaperServiceImpl implements OrderPaperService {

    @Autowired
    private OrderPaperMapper orderPaperMapper;
    @Autowired
    private OrderPaperQuestionMapper orderPaperQuestionMapper;
    @Autowired
    private OrderPaperController orderPaperController;

    //查询试卷表信息
    @Override
    public OrderPaper selectOrderPaperById(Integer id) {
        return orderPaperMapper.selectOrderPaperById(id);
    }

    // 查询试卷表列表
    @Override
    public List<OrderPaper> selectOrderPaperList(OrderPaper orderPaper) {
        return orderPaperMapper.selectOrderPaperList(orderPaper);
    }

    // 新增试卷表
    @Override
    public int insertOrderPaper(OrderPaper orderPaper) {
        return orderPaperMapper.insertOrderPaper(orderPaper);
    }

    @Override
    public int insertPaperTest(OrderPaperVo orderPaper) {
        return orderPaperMapper.insertOrderPaper(orderPaper);
    }

    //修改试卷表
    @Override
    public int updateOrderPaper(OrderPaper orderPaper) {
        return orderPaperMapper.updateOrderPaper(orderPaper);
    }

    //删除试卷表对象
    @Override
    public int deleteOrderPaperByIds(String[] ids) {
        return orderPaperMapper.deleteOrderPaperByIds(ids);
    }

    @Override
    public int deleteOrderPaperById(Integer id) {
        return orderPaperMapper.deleteOrderPaperById(id);
    }

    @Override
    public List<OrderPaper> selectOrderPaperList1() {
        return orderPaperMapper.selectOrderPaperList1();
    }

    @Override
    public List<OrderPaper> selectOrderPaperStuList(OrderPaper orderPaper) {
        return orderPaperMapper.selectOrderPaperStuList(orderPaper);
    }

    @Override
    public List<Integer> selectOrderPaperStuListAll(OrderPaper orderPaper) {
        return orderPaperMapper.selectOrderPaperStuListAll(orderPaper);
    }

    @Override
    public List<Integer> selectOrderPaperStuListSome(String courseId, Integer stuId) {
        return orderPaperMapper.selectOrderPaperStuListSome(courseId, stuId);
    }

    @Override
    public List<OrderPaper> selectOrderPaperStuListOther(List<Integer> other) {
        return orderPaperMapper.selectOrderPaperStuListOther(other);
    }

    @Override
    public List<OrderPaper> paperListByTeacher(Integer teaId, String paperName, Integer examStatus, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        return orderPaperMapper.paperListByTeacher(teaId, paperName, examStatus);
    }

    @Override
    public List<TClass> selectClassBypaperId(Integer paperId) {
        return orderPaperMapper.selectClassBypaperId(paperId);
    }

    @Override
    public List<TClass> selectClassByTopCatalogId(Integer topCatalogId) {
        return orderPaperMapper.selectClassByTopCatalogId(topCatalogId);
    }

    @Override
    public int selectWaitStudentPaperNumByTeaId(String teaId) {
        return orderPaperMapper.selectWaitStudentPaperNumByTeaId(teaId);
    }

    /**
     * 根据二级目录查试卷(查询老师准备的某门课程下的全部试题列表)
     *
     * @param twoIntList 二级目录列表
     * @return
     */
    @Override
    public List<Integer> selectpaperIdByLessonIdTwo(List<Integer> twoIntList) {
        return orderPaperMapper.selectpaperIdByLessonIdTwo(twoIntList);
    }

    @Override
    public int insertClassPaper(Integer classId, List<Integer> paperIdList, Integer teaId) {
        return orderPaperMapper.insertClassPaper(classId, paperIdList, teaId);
    }

    @Override
    public Integer insertClassPaper(List<Integer> classIdList, Integer paperId, String teaId) {
        return orderPaperMapper.insertClassPaperTwo(classIdList, paperId, teaId);
    }

    /**
     * 排除重复数据(根据老师id和班级id查找classpaper表得到paper列表id)
     *
     * @param classId
     * @param teaId
     * @return
     */
    @Override
    public List<Integer> queryPaperIdByClassIdAndTeaIdFromClassPaper(Integer classId, Integer teaId) {
        return orderPaperMapper.queryPaperIdByClassIdAndTeaIdFromClassPaper(classId, teaId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void clone(OrderPaper orderPaper) throws Exception {
        OrderPaper orderPaper1 = orderPaperMapper.selectOrderPaperById(orderPaper.getId());
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, 1);
        orderPaper1.setStartTime(c.getTime());
        c.add(Calendar.DAY_OF_MONTH, 1);
        orderPaper1.setEndTime(c.getTime());
        OrderPaperQuestion orderPaperQuestion = new OrderPaperQuestion();
        orderPaperQuestion.setPaperId(orderPaper.getId());
        List<OrderPaperQuestion> orderPaperQuestions = orderPaperQuestionMapper.selectOrderPaperQuestionList(orderPaperQuestion);
        orderPaper1.setId(null);
        orderPaper1.setCreateTime(DateUtils.getTime());
        orderPaper1.setIntroduction(orderPaper1.getIntroduction() + "_" + (int) (Math.random() * 9000 + 1000));
        orderPaperMapper.insertOrderPaper(orderPaper1);
        orderPaperController.insertPaperToClassPaper(orderPaper1.getId(), orderPaper1.getTopCatalogId(), orderPaper1.getCreateuser());
        Integer id = orderPaper1.getId();
        for (OrderPaperQuestion paperQuestion : orderPaperQuestions) {
            paperQuestion.setPaperId(id);
        }
        orderPaperQuestionMapper.insertBatch(orderPaperQuestions);
    }

    @Override
    public List<OrderPaperAppVO> selectPaperListForTea(Integer teaId) {
        return orderPaperMapper.selectPaperListForTea(teaId);
    }

    @Override
    public int deleteClassPaperByPaperId(int paperId) {
        return orderPaperMapper.deleteClassPaperByPaperId(paperId);
    }
}